package com.redisdemo;
import java.util.*;
import redis.clients.jedis.*;
//mvn exec:java -Dexec.mainClass="com.redisdemo.JedisSharedUtil" -Dexec.cleanupDaemonThreads=false
public class JedisSharedUtil {
	private static final ShardedJedisPool pool;
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("redis");
        if (bundle == null) {
            throw new IllegalArgumentException(
                "[redis.properties] is not found!");
        }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxActive(Integer.valueOf(bundle.getString(
                    "redis.pool.maxActive")));
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
        config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString(
                    "redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(bundle.getString(
                    "redis.pool.testOnReturn")));
        JedisShardInfo jedisShardInfo1 = new JedisShardInfo(bundle.getString("redis1.ip"),
            Integer.valueOf(bundle.getString("redis.port")));
        JedisShardInfo jedisShardInfo2 = new JedisShardInfo(  
                bundle.getString("redis2.ip"), Integer.valueOf(bundle.getString("redis.port")));
        List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
        list.add(jedisShardInfo1);
        list.add(jedisShardInfo2);
        pool = new ShardedJedisPool(config, list);
    }
    /**
     * 通过以上方式，向redis进行set操作的key-value，会通过hash而均匀的分配到pool里的redis机器中。
     */
    public static void main(String []args){
        ShardedJedis jedis = pool.getResource();  
        String keys = "name1";  
        String value = "snowolf";  
        // 删数据  
        //jedis.del(keys);  
        // 存数据  
        //jedis.set(keys, value);  
        //jedis.set("name1", "wangjun1");
        //jedis.set("id", "123456");
        //jedis.set("address", "guangzhou");
        // 取数据  
        String v = jedis.get(keys);  
        System.out.println(v);  
        // 释放对象池  
        pool.returnResource(jedis);
    }
}